import {defineStore} from 'pinia'
import interaction from "@/util/Interaction";

export const baseStore = defineStore('baseStore', {
    state: () => {
        return {
            localState: null,
            menuFoldState: true,
            fullscreenState: false,
            menuTab: [],
            token: null,
            update: false,
            expired: false,
            // menuState 必须为{ menu:[],button:[] }对象，其中menu为菜单，button为按钮
            menuState: {
                menu:[],
                button:[]
            },
            userKey: null
        }
    },
    getters: {
        getLocalState() {
            return this.localState
        }
    },
    actions: {
        /**
         * 两个不为tree结构的路由,会返回一个带tree的路由
         * @param rearRouter 后端维护的权限集合
         * @param frontRouter 前端维护的路由集合
         */
        async formatRouter(rearRouter, frontRouter) {
            this.menuState = {
                menu:[],
                button:[]
            }
            try {
                let button = []
                if (rearRouter == null || rearRouter.length < 1) {
                    this.menuState = {
                        menu:frontRouter,
                        button:button
                    }
                    return
                }
                let menu = await interaction.treeToFlat(frontRouter, 'children')
                let list = await rearRouter.map(rear => {
                    delete rear.children
                    let front = menu.find(front => front.path === rear.path)
                    if (front) {
                        rear = Object.assign(front, rear)
                        return rear;
                    }
                    // 不是菜单就是按钮
                    this.menuState.button.push(rear.path)
                    return null;
                }).filter(item => item != null)
                this.menuState.menu = await interaction.flatToTree(list, 'id', 'parent', 'children')
            }catch(e ){
                console.log(e)
            }
        },
        computeStorage(aString, extractLen = 16, seed) {
            // 检查输入参数合法性
            if (aString === null || aString.length < extractLen) {
                return null;
            }
            // 线性同余生成器(LCG)参数，与Java版本保持一致
            const modulus = 4294967296; // 2^32
            const a = 1664525;
            const c = 1013904223;
            let current = seed;
            const indices = new Set();
            // 生成不重复的索引
            while (indices.size < extractLen) {
                current = (a * current + c) % modulus;
                // 映射到字符串长度范围内的索引
                const index = Math.floor(current % aString.length);
                indices.add(index);
            }
            // 排序索引并提取字符
            const sortedIndices = Array.from(indices).sort((a, b) => a - b);
            let subString = '';
            for (const index of sortedIndices) {
                subString += aString.charAt(index);
            }
            this.userKey = subString
        },
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'BASE_STORE',
                storage: localStorage,
                paths: ['localState', 'token', 'menuFoldState', 'menuState','update','expired']
            },
            {
                key: 'BASE_STORE',
                storage: sessionStorage,
                paths: ['userKey']
            }
        ]
    }
})
